<template>
	<el-dialog v-model="visible" :title="'审核'" :close-on-click-modal="false">
		<el-tabs v-model="activeName" type="card">
			<el-tab-pane label="新增审核" name="AddExamine" :disabled="isAddExamine"> <sup style="color: red">*</sup>已经在测试库运行成功 </el-tab-pane>
			<el-tab-pane label="停用审核" name="StopExamine" :disabled="isStopExamine">
				<el-form ref="stopFormRef" :model="stopForm" label-width="100px">
					<el-form-item label="停用理由">
						<el-input v-model="stopForm.ycyy" :rows="2" type="textarea" autocomplete="off"></el-input>
					</el-form-item>
				</el-form>
			</el-tab-pane>
			<el-tab-pane label="审核记录" name="ExamineList">
				<el-table v-loading="listLoading" :data="tableData" border style="width: 100%">
					<el-table-column prop="fqr" label="发起人" align="center" />
					<el-table-column prop="reviewer" label="审核人" align="center" />
					<el-table-column prop="shlx" label="审核类型" width="150" align="center" />
					<el-table-column prop="shbm" label="审核部门" align="center" />
					<el-table-column prop="shbz" label="是否通过" width="150" align="center" />
					<el-table-column prop="shsj" label="审核时间" align="center">
						<template #default="scope">
							{{ format(scope.row.shsj) }}
						</template>
					</el-table-column>
				</el-table>
			</el-tab-pane>
		</el-tabs>
		<template #footer>
			<div v-if="activeName === 'AddExamine'" style="float: left; line-height: 31px">
				<el-checkbox v-model="execute">标签生效后立即执行一次</el-checkbox>
			</div>

			<el-button @click="goBack">{{ activeName === 'ExamineList' ? '关 闭' : '取 消' }}</el-button>
			<el-button type="primary" v-if="activeName === 'AddExamine' && execute" @click="dataFormSubmit()"> 确 定</el-button>
			<el-button type="danger" v-if="activeName === 'StopExamine'" @click="stopFormSubmit()"> 确定停用</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { getProcessList, addProcess, combinationStop, stopProcess } from '@/api/labels-mana/index'
import { ElMessage } from 'element-plus/es'
import timeFormater from 'time-formater'

const emit = defineEmits(['close'])

const visible = ref(false)
const activeName = ref('ExamineList')
const isAddExamine = ref(false)
const isStopExamine = ref(false)
const continueBtnLoading = ref(false)
const execute = ref(false)

const listLoading = ref(false)

const stopForm = reactive({
	ycyy: ''
})

const tableData = ref([])
const zhbh = ref('')
const type = ref('')
const stopFormRef = ref()

const format = data => {
	return timeFormater(data).format('YYYY-MM-DD')
}

const init = (data, typeName) => {
	if (type) {
		type.value = typeName
	}
	visible.value = true
	console.log(data)
	switch (data.shbz) {
		case '0':
			isAddExamine.value = false
			isStopExamine.value = true
			break
		case '1':
			isAddExamine.value = true
			isStopExamine.value = false
			break
		default:
			isAddExamine.value = true
			isStopExamine.value = true
			break
	}
	activeName.value = 'ExamineList'
	listLoading.value = false
	zhbh.value = !type.value ? data.zhbh : data.bqid

	getTableData()
}

const getTableData = () => {
	getProcessList({ bqid: zhbh.value }).then(res => {
		tableData.value = res.data
	})
}

const dataFormSubmit = () => {
	let data = {}
	if (type.value) {
		data = {
			bqid: zhbh.value
		}
	} else {
		data = {
			zhbh: zhbh.value
		}
	}

	addProcess(data, type.value)
		.then(res => {
			visible.value = false
			if (res.data.bz === '2') {
				ElMessage({
					message: '审核成功',
					type: 'success',
					duration: 500,
					onClose: () => {
						emit('close')
					}
				})
			} else {
				ElMessage({
					message: res.data.message,
					type: 'error',
					duration: 500,
					onClose: () => {
						emit('close')
					}
				})
			}
		})
		.catch(() => {})
}

const stopFormSubmit = () => {
	if (type.value) {
		// 基础标签
		const data = {
			bqid: zhbh.value,
			shbz: 4,
			shr: JSON.parse(sessionStorage.getItem('userInfo')).id,
			shsj: timeFormater(new Date()).format('YYYY-MM-DD HH:mm:ss'),
			ycyy: stopForm.ycyy
		}

		stopProcess(data).then(res => {
			if (res.data.bz === '2') {
				ElMessage({
					message: '审核成功',
					type: 'success',
					duration: 500,
					onClose: () => {
						visible.value = false
						emit('close')
					}
				})
			} else {
				ElMessage({
					message: res.data.message,
					type: 'error',
					duration: 500,
					onClose: () => {
						visible.value = false
						emit('close')
					}
				})
			}
		})
	} else {
		// 组合标签
		const data = {
			zhbh: zhbh.value,
			shbz: 4,
			reviewer: JSON.parse(sessionStorage.getItem('userInfo')).id,
			ycyy: stopForm.ycyy
		}
		combinationStop(data).then(res => {
			if (res.data.bz === '2') {
				ElMessage({
					message: '审核成功',
					type: 'success',
					duration: 500,
					onClose: () => {
						visible.value = false
						emit('close')
					}
				})
			} else {
				ElMessage({
					message: res.data.message,
					type: 'error',
					duration: 500,
					onClose: () => {
						visible.value = false
						emit('close')
					}
				})
			}
		})
	}
}

const goBack = () => {
	visible.value = false
}

defineExpose({
	init
})
</script>
<style lang="sass" scoped>
</style>